import { reqGetCode, reqUserRegister } from '@/api'
import { USER_REGISTER, GET_CODE } from '@/store/mutation-types'

export default {
  namespaced: true,
  state: {
    userCode: '',
  },
  actions: {
    async getCode({ commit }, phone) {
      try {
        const res = await reqGetCode(phone)
        if (res.code == 200) {
          commit(GET_CODE, res.data)
          return Promise.resolve('获取成功')
        }
        throw (res.message)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    async userRegister({ commit }, data) {
      try {
        const res = await reqUserRegister(data)
        if (res.code == 200) {
          commit(USER_REGISTER, res.data)
          return Promise.resolve('注册成功')
        }
        throw (res.message)
      } catch (error) {
        return Promise.reject(error)
      }
    },
  },
  mutations: {
    [GET_CODE]: (context, data) => context.userCode = data,
    [USER_REGISTER]: (context, data) => context.userInfo = data
  },
  getters: {}
}